package com.toefl.speaking.helper;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ihandy.core.Constants;
import com.ihandy.core.util.IsUtil;
import com.toefl.speaking.util.StringUtils;

public class DialogHelper {
	/**
	 * 首次登录
	 * @param ctx
	 * @param sn号
	 * @param simId SIM卡号
	 * @param onOK
	 */
	public static void firstLogin(final Context ctx, String sn) {
		StringBuffer sb = new StringBuffer();
		sb.append("当前设备SN号:");
		sb.append(sn);
		showToastCanceledOnTouchOutside(ctx, "设备未开通", sb.toString());
	}

	/**
	 * 获得进度条
	 * 
	 * @param ctx
	 * @param title
	 * @return
	 */
	public static ProgressDialog getProgress(final Context ctx, String title) {
		ProgressDialog mProgressDialog = new ProgressDialog(ctx);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mProgressDialog.setIndeterminate(true);// 设置进度条是否不明确
		mProgressDialog.setMessage(Constants.LOADING);
		if (!IsUtil.isNullOrEmpty(title)) {
			mProgressDialog.setTitle(title);
		}
		return mProgressDialog;
	}

	/**
	 * 显示提示信息
	 * 
	 * @param resId
	 */
	public static void showTips(final Context ctx, Bitmap res) {
		final AlertDialog tips_dialog = new AlertDialog.Builder(ctx).create();
		Window win = tips_dialog.getWindow();
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.x = 440;
		lp.y = -120;
		tips_dialog.show();
		tips_dialog.setCancelable(true);
		tips_dialog.setCanceledOnTouchOutside(true);
		ImageView tips = new ImageView(ctx);
		tips.setImageBitmap(res);
		tips_dialog.setContentView(tips, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	}

	/***
	 * 显示提示消息
	 * @param ctx
	 * @param title 消息提示框的 标题 ，如果没有传入此参数，则默认标题是“提示”
	 * @param msg 要提示的消息内容
	 * @param okBtnLabel 确认按钮上的文字 “Label”
	 * @param okcallBack 确定按钮的回调接口
	 */
	public static void showTipsWithOnClickCallBack(final Context ctx, String title, String msg, String okBtnLabel,
			DialogInterface.OnClickListener okcallBack) {
		if (StringUtils.isEmpty(title)) {
			title = "提示";
		}
		if (StringUtils.isBlank(okBtnLabel)) {
			okBtnLabel = "确定";
		}
		new AlertDialog.Builder(ctx).setTitle(title).setMessage(msg).setPositiveButton(okBtnLabel, okcallBack)
				.setNegativeButton("取消", null).show();
	}

	/***
	 * 显示提示消息
	 * @param ctx
	 * @param title 消息提示框的 标题 ，如果没有传入此参数，则默认标题是“提示”
	 * @param msg 要提示的消息内容
	 */
	public static void showToastCanceledOnTouchOutside(final Context ctx, String title, String msg) {
		if (StringUtils.isEmpty(title)) {
			title = "提示";
		}

		final Dialog dialog = new AlertDialog.Builder(ctx).setTitle(title)
				.setIcon(android.R.drawable.ic_menu_info_details).setMessage(msg)
				.setPositiveButton("确定", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create();
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
	}

	/***
	 * 显示提示消息
	 * @param ctx
	 * @param title 消息提示框的 标题 ，如果没有传入此参数，则默认标题是“提示”
	 * @param msg 要提示的消息内容
	 */
	public static void showToastCanceledOnTouchOutside(final Context ctx, String title, String msg,
			OnClickListener positiveListener) {
		if (StringUtils.isEmpty(title)) {
			title = "提示";
		}

		if (positiveListener == null) {
			positiveListener = new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			};
		}

		final Dialog dialog = new AlertDialog.Builder(ctx).setTitle(title)
				.setIcon(android.R.drawable.ic_menu_info_details).setMessage(msg)
				.setPositiveButton("确定", positiveListener).create();
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
	}

	/***
	 * 显示提示消息
	 * @param ctx
	 * @param title 消息提示框的 标题 ，如果没有传入此参数，则默认标题是“提示”
	 * @param msg 消息内容 只有点击确定后才会关闭
	 */
	public static void showToast2(final Context ctx, String title, String msg) {
		if (StringUtils.isEmpty(title)) {
			title = "提示";
		}

		final Dialog dialog = new AlertDialog.Builder(ctx).setTitle(title)
				.setIcon(android.R.drawable.ic_menu_info_details).setMessage(msg)
				.setPositiveButton("确定", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create();
		dialog.show();
	}

	/***
	 * 显示提示消息
	 * @param ctx
	 * @param title 消息提示框的 标题 ，如果没有传入此参数，则默认标题是“提示”
	 * @param msg 消息内容 只有点击确定后才会关闭
	 */
	public static void showToastException(final Context ctx, String title, String msg) {
		if (StringUtils.isEmpty(title)) {
			title = "提示";
		}
		ScrollView view = new ScrollView(ctx);
		TextView textView = new TextView(ctx);
		textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		textView.setTextSize(25);
		textView.setText(msg);
		view.addView(textView);
		final Builder dialog = new AlertDialog.Builder(ctx);
		dialog.setTitle(title);
		dialog.setIcon(android.R.drawable.ic_menu_info_details);
		dialog.setPositiveButton("确定", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		dialog.setView(view);
		dialog.create();
		dialog.show();
	}

	// /**
	// * 创建自定义对话框
	// * @param context
	// * @param title
	// * @param msg
	// */
	// private static void buildCustomDialog(final Context context, String title, String msg,
	// final boolean isNeedPopBrithCertDialog) {
	// if (StringUtils.isEmpty(title)) {
	// title = "提示";
	// }
	// View view = LayoutInflater.from(context).inflate(R.layout.birthprove, null);
	// final CustomDialog dialog = new CustomDialog(context, R.style.dialog, view);// 创建Dialog并设置样式主题
	// TextView tv = (TextView) view.findViewById(R.id.birthprove_msg);
	// TextView titleView = (TextView) view.findViewById(R.id.dialog_title);
	// titleView.setText(title);
	// tv.setText(msg);
	// Button button = (Button) view.findViewById(R.id.birthprove_btn);
	//
	// button.setOnClickListener(new android.view.View.OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// dialog.dismiss();
	// if (isNeedPopBrithCertDialog) {
	// showBirthProve(context);
	// }
	// }
	// });
	// dialog.show();
	// }

	// // 四岁以下需提供出生证明提示功能
	// public static void showBirthProve(Context ctx) {
	// String msg = ctx.getString(R.string.prompt_msg_birth_certification);
	// View view = LayoutInflater.from(ctx).inflate(R.layout.birthprove, null);
	// final CustomDialog dialog = new CustomDialog(ctx, R.style.dialog, view);// 创建Dialog并设置样式主题
	// TextView tv = (TextView) view.findViewById(R.id.birthprove_msg);
	// tv.setText(msg);
	// Button button = (Button) view.findViewById(R.id.birthprove_btn);
	// button.setOnClickListener(new android.view.View.OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// dialog.dismiss();
	// }
	// });
	// dialog.show();
	// }
}
